package gonz.data.swim;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class Image {
    public static final Logger log = Logger.getLogger(Image.class);
    private String link;
    private String name;
    private String ext;
    private String color;
    private Top top;
    private String localName;
    private static final char[] FOLDER_ILLEGAL_CHARACTERS = {'\n', '\r', '\t', '\0', '\f', '`', '?', '*', '<', '>', '|', '\"', ':'};
    private static final char[] ILLEGAL_CHARACTERS = {'/', '\n', '\r', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':'};
    public static final char ILLEGAL_REPLACER = ' ';

    public Image(String link, Top top) {
        this.link = link;
        this.top = top;
        this.ext = StringUtils.substringAfterLast(link, ".");
        this.name = StringUtils.substringBeforeLast(StringUtils.substringAfterLast(link, "/"), ".");

    }

    public String getLink() {
        return link;
    }

    public String getName() {
        return name;
    }

    public String getExt() {
        return ext;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    public Top getTop() {
        return top;
    }

    public String getLocalName() {
        return localName;
    }

    public void download(String dest) {
        try {
            URL pictureUrl = new URL(link);
            InputStream inputStream = pictureUrl.openStream();
            this.localName = removeIllegalCharsFromDir(dest)
                    + File.separator +
                    removeIllegalChars(getTop().getArticle() + " " + getColor() + "." + getExt());


            OutputStream outputStream = new BufferedOutputStream(
                    new FileOutputStream(getLocalName()));
            for (int b; (b = inputStream.read()) != -1; ) {
                outputStream.write(b);
            }
            outputStream.close();
            inputStream.close();

        } catch (MalformedURLException e) {
            log.error("MalformedURLException  URL from link: " + link + ", swim link:"
                    + getTop().getSwim().getLink(), e);
        } catch (IOException e) {
            log.error("IOException open URL stream from link " + link + ", swim link:"
                    + getTop().getSwim().getLink(), e);
        }
    }

    private String removeIllegalChars(String str) {
        StringUtils.replace(str, "/", File.separator);
        StringUtils.replace(str, "\\", File.separator);

        for (int i = 0; i < ILLEGAL_CHARACTERS.length; i++) {
            str = str.replace(ILLEGAL_CHARACTERS[i], ILLEGAL_REPLACER);
        }

        return str;
    }

    private String removeIllegalCharsFromDir(String str) {
        StringUtils.replace(str, "/", File.separator);
        StringUtils.replace(str, "\\", File.separator);

        for (int i = 0; i < FOLDER_ILLEGAL_CHARACTERS.length; i++) {
            str = str.replace(FOLDER_ILLEGAL_CHARACTERS[i], ILLEGAL_REPLACER);
        }

        return str;
    }
}
